package com.summer.user.center.controller;

import com.github.pagehelper.PageInfo;
import com.summer.common.FileConfig;
import com.summer.common.file.FileSystemFactory;
import com.summer.common.model.AjaxResult;
import com.summer.user.center.model.User;
import com.summer.user.center.model.UserAndInfoModel;
import com.summer.user.center.model.UserAndInfoVo;
import com.summer.user.center.model.UserInfoModel;
import com.summer.user.center.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private FileSystemFactory fileSystemFactory;
    @Autowired
    private FileConfig fileConfig;

    /**
     * 根据用户名获取用户密码和id
     *
     * @param username
     * @return
     */
    @RequestMapping("/findUserByUsername")
    public User findUserByUsername(String username) {
        Assert.notNull(username, "用户名不能为空");
        User user = userService.findUserByUsername(username);
        return user;
    }

    /**
     * 获取用户所拥有的权限资源
     *
     * @param username
     * @return
     */
    @RequestMapping("/findResourceByUsername")
    public HashSet<String> findResourceByUsername(String username) {
        Assert.notNull(username, "用户名不能为空");
        return userService.findResourceByUsername(username);
    }

    /**
     * 根据用户名查找用户信息
     *
     * @param username
     * @return
     */
    @RequestMapping("/findUserInfoByUsername")
    public UserInfoModel findUserInfoByUsername(String username) {
        Assert.notNull(username, "用户名不能为空");
        return userService.findUserInfoByUsername(username);
    }

    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @RequestMapping("/findPage")
    public PageInfo<UserAndInfoModel> findPage(UserAndInfoVo vo) {
        return userService.findPage(vo);
    }

    /**
     * 查询用户名是否已使用
     *
     * @param username
     * @param id
     * @return
     */
    @RequestMapping("/usernameOccupation")
    public AjaxResult usernameOccupation(String username, Long id) {
        return new AjaxResult(userService.usernameOccupation(username, id));
    }

    /**
     * 添加用户
     *
     * @param vo
     * @return
     */
    @RequestMapping("/addUser")
    public AjaxResult addUser(UserAndInfoVo vo) {
        return new AjaxResult(userService.addUser(vo));
    }

    /**
     * 查找用户部门id
     *
     * @param userId
     * @return
     */
    @RequestMapping("/findDeptId")
    public AjaxResult findDeptId(String userId) {
        if (userId == null) {
            return new AjaxResult(false);
        }
        Long deptId = userService.findDeptId(userId);
        return new AjaxResult(deptId != null, deptId);
    }

    /**
     * 更新用户信息
     *
     * @return
     */
    @RequestMapping("/updateUser")
    public AjaxResult updateUser(UserAndInfoVo vo) {
        if (vo.getId() == null) {
            return new AjaxResult(false, "用户id不能为空");
        }
        return new AjaxResult(userService.updateUser(vo));
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteUser")
    public AjaxResult deleteUser(Long id) {
        if (id == null) {
            return new AjaxResult(false, "用户id不能为空");
        }
        return new AjaxResult(userService.deleteUser(id));
    }

    /**
     * 重置用户密码
     *
     * @param id
     * @return
     */
    @RequestMapping("/rePassword")
    public AjaxResult rePassword(Long id) {
        if (id == null) {
            return new AjaxResult(false, "用户id不能为空");
        }
        return new AjaxResult(userService.rePassword(id));
    }

    /**
     * 查询用户的所有角色
     *
     * @return
     */
    @RequestMapping("/findRoleIds")
    public AjaxResult findRoleIds(Long userId) {
        if (userId == null) {
            return new AjaxResult(false, "用户id不能为空");
        }
        List<Long> roleIds = userService.findRoleIdsByUserId(userId);
        return new AjaxResult(true, roleIds);
    }

    /**
     * 用户头像上传
     *
     * @param file
     * @param userId
     * @return
     */
    @RequestMapping("/uploadHeadImage")
    public AjaxResult uploadHeadImage(@RequestParam("headImage") MultipartFile file, Long userId) throws IOException {
        if (file.isEmpty() && userId != null) {
            return new AjaxResult(false);
        }

        String originalFilename = file.getOriginalFilename();
        String filePath = fileConfig.getHeadImagePath() + System.currentTimeMillis() + originalFilename;
        fileSystemFactory.getFileSystem().write(filePath, file.getInputStream());

        userService.updateHeadSculpture(userId,filePath);

        return new AjaxResult(true, filePath);
    }

    @RequestMapping("/editInfo")
    public AjaxResult editInfo(UserAndInfoModel user){
        Assert.notNull(user.getId(),"用户id不能为空");
        userService.editInfo(user);
        return new AjaxResult(true);
    }
}
